package com.xzl.shop.controller;

import com.github.pagehelper.PageHelper;
import com.xzl.manger.model.Order;
import com.xzl.manger.model.OrderGood;
import com.xzl.manger.server.OrderServer;
import com.xzl.shop.model.Cart;
import com.xzl.shop.model.Goods2;
import com.xzl.shop.model.GoodsType2;
import com.xzl.shop.model.User;
import com.xzl.shop.server.CartServer;
import com.xzl.shop.server.Goods2Server;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import redis.clients.jedis.Jedis;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("shopping")
public class ShopController {
    @Autowired
    private OrderServer orderServer;
    @Autowired
    private Goods2Server goods2Server;
    @Autowired
    private CartServer cartServer;
    @Autowired
    private Jedis jedis;
    @RequestMapping("main")
    public ModelAndView tonewpage(User user, HttpServletRequest request){
        ModelAndView mv = new ModelAndView();
        mv.addObject("user",user);
        mv.setViewName("shop/main");
        HttpSession session = request.getSession();
        mv.addObject("username",session.getAttribute("username"));
        return mv;
    }
    public List<Goods2> queryNew(){
        Long len = jedis.dbSize();
        List<Goods2> newlist = new ArrayList<>();
        for (int i = 0; i < len; i++){
            List<String> list = jedis.hmget("h"+i,"id","name","price","amount","url");
            Goods2 good2 = new Goods2();
            good2.set(list);
            newlist.add(good2);
        }
        return newlist;
    }
    @RequestMapping("homepage")
    public ModelAndView toHomePage(Goods2 good){

        ModelAndView mv = new ModelAndView();
        List<Goods2> newlist = queryNew();
        mv.addObject("newlist",newlist);
        List<Goods2> goodlist = goods2Server.selectGoodsOnSale(good);
        mv.addObject("goodlist",goodlist);
        List<GoodsType2> goodsTypes = goods2Server.selectTypes();
        mv.addObject("goodstypes",goodsTypes);
        mv.setViewName("shop/homepage");
        return mv;
    }
    @RequestMapping("list")
    public ModelAndView toList(Goods2 good){
        ModelAndView mv = new ModelAndView();
        if(good.getName()==null&&good.getType()==null)
            return toHomePage(good);
        if(good.getName().isEmpty()&&good.getType()==null)
            return toHomePage(good);
        List<Goods2> goodlist = goods2Server.selectGoodsOnSale(good);
        mv.addObject("goodlist",goodlist);
        List<GoodsType2> goodsTypes = goods2Server.selectTypes();
        mv.addObject("goodstypes",goodsTypes);
        mv.setViewName("shop/list");
        return mv;
    }
    @RequestMapping("good")
    public ModelAndView toGood(Goods2 good){
        ModelAndView mv = new ModelAndView();
        List<Goods2> list = goods2Server.selectGoods(good);
        mv.addObject("good",list.get(0));
        mv.setViewName("shop/good");
        return mv;
    }
    @RequestMapping("tobuy")
    public ModelAndView tobuy(Goods2 goods2){
        ModelAndView mv = new ModelAndView();
        List<Goods2> list = goods2Server.selectGoods(goods2);
        mv.addObject("good",list.get(0));
        mv.setViewName("shop/buy");
        return mv;
    }
    @RequestMapping("creorder")
    public void createorder(String phone, Integer count, Integer id, BigDecimal price, String name, Integer uId){
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        String orderId = sdf.format(date) + phone.substring(phone.length()-4,phone.length());
        OrderGood orderGood = new OrderGood();
        orderGood.setOrderId(orderId);
        orderGood.setCount(count);
        orderGood.setGId(id);
        orderGood.setGPrice(price);
        orderGood.setGName(name);
        orderGood.setMoney(price.multiply(BigDecimal.valueOf(count)));
        Integer i1 = orderServer.insertOrderGood(orderGood);
        Order order = new Order();
        order.setOrderId(orderId);
        order.setUserId(uId);
        order.setOrderMoney(price.multiply(BigDecimal.valueOf(count)));
        Integer i2 = orderServer.insertOrder(order);
        System.err.println("创建订单"+i1+" "+i2);
    }
    @RequestMapping("addCart")
    public void addCart(Integer count, Integer uId, Integer id, BigDecimal price, String name){
        Cart cart = new Cart();
        cart.setGId(id);
        cart.setGName(name);
        cart.setGPrice(price);
        cart.setCount(count);
        cart.setUserId(uId);
        cart.setMoney(price.multiply(BigDecimal.valueOf(count)));
        cartServer.addCart(cart);
    }
}
